package com.cxx.utils;

import com.cxx.annotation.Service;

import java.sql.Connection;
import java.sql.SQLException;

/**
 * TODO
 *
 * @author Shuhang He
 * @version 1.0
 * @date 2021/3/14 21:23
 */
@Service
public class ConnectionUtils {


    private final ThreadLocal<Connection> threadLocal = new ThreadLocal<>();


    /**
     * 获取线程内的Connection对象
     * @return
     * @throws SQLException
     */
    public Connection getCurrentThreadConn() throws SQLException {
        Connection connection = threadLocal.get();
        if (connection == null) {
            connection = DruidUtils.getInstance().getConnection();
            threadLocal.set(connection);
        }
        return connection;
    }


}
